package com.iotechn.unimall.admin.api.userShop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopUserDO;
import com.iotechn.unimall.data.domain.UserShopDO;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class AdminShopUserServiceImpl implements AdminShopUserService {
    @Autowired
    private ShopUserMapper shopUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopUserDO create(ShopUserDO userShopDO, Long adminId) throws ServiceException {
        Date now = new Date();
        userShopDO.setGmtCreate(now);
        userShopDO.setGmtUpdate(now);
        if (shopUserMapper.insert(userShopDO) > 0) {

            userShopDO.setId(userShopDO.getId());
            return userShopDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopUserDO userShopDO, Long adminId) throws ServiceException {
        Long id = userShopDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        userShopDO.setGmtUpdate(now);
        if (shopUserMapper.updateById(userShopDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopUserMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }


}